const baseURL = 'http://localhost:8080';

const server = (options) => {
	return new Promise((resolve, reject) => {
		// --- Request Interceptor Logic ---
		const token = uni.getStorageSync('token');
		let header = options.header || {};
		if (token) {
			header['token'] = token;
		} else {
			console.log("token为空");
		}

		uni.request({
			url: baseURL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: header,
			success: (res) => {
				// --- Response Interceptor Logic ---
				if (res.data.code === 401) {
					console.log("token为空");
					// Optionally, redirect to login
				} else if (res.data.code === 500) {
					console.log("token错误");
					uni.removeStorageSync('token');
					uni.navigateTo({
						url: '/pages/login/index'
					});
				}
				// Resolve the promise with the response
				resolve(res);
			},
			fail: (err) => {
				console.warn("响应拦截器失败", err);
				reject(err);
			}
		});
	});
};

export { server };
